package org.particlethink.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.particlethink.auth.DefaultUser;
import org.particlethink.context.DefaultContext;
import org.particlethink.mapper.SupplierMapper;
import org.particlethink.model.Supplier;
import org.particlethink.model.User;
import org.particlethink.response.BaseResponse;
import org.particlethink.response.SupplierResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "供应商")
@RestController
@RequestMapping("/supplier")
public class SupplierController {


    @Autowired
    public SupplierMapper supplierMapper;

    @Operation(summary = "供应商列表")
    @GetMapping
    public BaseResponse<List<SupplierResponse>> P(@RequestParam(value = "name", required = false) String name) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        DefaultUser defaultUser = (DefaultUser) authentication.getPrincipal();
        User user = defaultUser.getUser();

        List<Supplier> suppliers = supplierMapper.SelectByName(user.getToken(), name);
        return BaseResponse.success(suppliers.stream().map(SupplierResponse::new).collect(Collectors.toList()));
    }

}
